<?php

namespace app\api\logic;

use app\api\controller\User;
use app\common\basics\Logic;
use app\data\model\DataUser;
use think\facade\Db;

class UserLogic extends Logic
{
//设置个人信息
    public static function setUserInfo($post)
    {
        try{
            $field = $post['field'];
            $value = $post['value'];
            $info = DataUser::where(['id'=> $post['user_id']])->findOrEmpty();
            if(empty($info)) {
                self::$error = "用户不存在";
                return false;
            }
            $has_improve_avator = $info['has_improve_avator'];
            $has_improve_name = $info['has_improve_name'];
            if($post['field'] == "headimg"){
                if($info['has_improve_avator'] == 0){
                    $has_improve_avator = 1;
                }
            }
            if($post['field'] == "nickname"){
                if($info['has_improve_name'] == 0 && $post['value'] != "微信用户"){
                    $has_improve_name = 1;
                }
            }
            DataUser::mk()->where(['id'=> $post['user_id']])
                ->update([
                    $field => $value,
                    'has_improve_name'=>$has_improve_name,
                    'has_improve_avator'=>$has_improve_avator
                ]);
            return true;
        }catch(\Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
    }

    //更新微信信息
    public static function updateWechatInfo($user_id, $post)
    {
        try{
            $time = time();
            $avatar_url = $post['avatar'];
            $nickanme  = $post['nickname'];
            $sex = $post['sex'];
            DataUser::mk()->where(['id' => $user_id])->update([
                'nickname'  => $nickanme,
                'avatar' => $avatar_url,
                'sex' => $sex
            ]);
            return true;

        } catch(\Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
    }
}
